//
//  FireControlViewController.m
//  Qihuo
//
//  Created by Vencoo on 13-12-11.
//  Copyright (c) 2013年 Vencoo. All rights reserved.
//

#import "FireControlViewController.h"
#import "PickerBackgroundView.h"
@interface FireControlViewController ()<buttonDelegate>
{
    PickerBackgroundView *pickerView;
    UITextField *timeTextField;
    NSMutableArray *sendControls;
}
@end

@implementation FireControlViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    self.title = @"消防系统联动测试";
    [super viewDidLoad];
    self.checkXunJianArea = @"6";
    [Appinfomation shareIstance].strongXunJianCount = 250;
    UIImage *calenderBtnImage = [UIImage imageNamed:@"日历按钮"];
    UIImage *backgroundImage = [UIImage imageNamed:@"日历下拉底框"];
    self.myTableView.frame = CGRectMake(interval111/2, 140, self.myTableView.frame.size.width, self.myTableView.frame.size.height - (140-64));
    self.whiteImageView.frame = CGRectMake(self.myTableView.frame.size.width + self.myTableView.frame.origin.x, self.myTableView.frame.origin.y, self.whiteImageView.frame.size.width, self.whiteImageView.frame.size.height - (140-64));
    self.backScrollV.frame = CGRectMake(10, 10, self.whiteImageView.frame.size.width - 20, self.whiteImageView.frame.size.height - 20);

//    UILabel *checkClassLabel = [UILabel new];
//    checkClassLabel.text = @"检查种别：";
//    checkClassLabel.textColor = [UIColor whiteColor];
//    checkClassLabel.font = [UIFont systemFontOfSize:38];
//    CGSize mySz = [checkClassLabel.text sizeWithFont:checkClassLabel.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
//    checkClassLabel.frame = CGRectMake(interval111, interval111 + 60, mySz.width, mySz.height);
//    [self.view addSubview:checkClassLabel];
//    
//    UITextField *firstTf = [[UITextField alloc]initWithFrame:CGRectMake(checkClassLabel.frame.origin.x + checkClassLabel.frame.size.width, checkClassLabel.frame.origin.y, 150, mySz.height)];
//    firstTf.background = backgroundImage;
//    [self.view addSubview:firstTf];
    
    UILabel *alarmLb = [UILabel new];
    alarmLb.text = @"检测区域：";
    alarmLb.font = [UIFont systemFontOfSize:38];
    alarmLb.textColor = [UIColor whiteColor];
    CGSize mySz = [alarmLb.text sizeWithFont:alarmLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    alarmLb.frame = CGRectMake(interval111, interval111 + 60, mySz.width, mySz.height);
    [self.view addSubview:alarmLb];
    
    UITextField *secondTf = [[UITextField alloc]initWithFrame:CGRectMake(alarmLb.frame.origin.x + alarmLb.frame.size.width, alarmLb.frame.origin.y ,200, mySz.height)];
    secondTf.background = backgroundImage;
    secondTf.delegate = self;
    secondTf.font = [UIFont systemFontOfSize:25];
    secondTf.textColor = [UIColor whiteColor];
    [self.view addSubview:secondTf];
    secondTf.tag = 101;
    
//    UILabel *checkReportLb = [UILabel new];
//    checkReportLb.text = @"检查报告";
//    checkReportLb.font = [UIFont systemFontOfSize:38];
//    mySz = [checkReportLb.text sizeWithFont:checkReportLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
//    checkReportLb.textColor = [UIColor whiteColor];
//    checkReportLb.frame = CGRectMake(secondTf.frame.origin.x + secondTf.frame.size.width + 10, secondTf.frame.origin.y, mySz.width, mySz.height);
//    [self.view addSubview:checkReportLb];
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    timeTextField = [[UITextField alloc]initWithFrame:CGRectMake(600, alarmLb.frame.origin.y , 300, mySz.height)];
    timeTextField.background = backgroundImage;
    timeTextField.enabled = NO;
    timeTextField.text = [dateFormatter stringFromDate:[NSDate date]];
    timeTextField.font = [UIFont systemFontOfSize:25];
    timeTextField.textColor = [UIColor whiteColor];
    timeTextField.textAlignment = 1;
    [self.view addSubview:timeTextField];
    
    UIButton *clenderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [clenderBtn setImage:calenderBtnImage forState:UIControlStateNormal];
    [clenderBtn addTarget:self action:@selector(showCalenderSelectView:) forControlEvents:UIControlEventTouchUpInside];
    clenderBtn.frame = CGRectMake(timeTextField.frame.origin.x + timeTextField.frame.size.width + 10, timeTextField.frame.origin.y - 20, calenderBtnImage.size.width, calenderBtnImage.size.height);
    [self.view addSubview:clenderBtn];
    
    pickerView = [[PickerBackgroundView alloc]initWithFrame:CGRectMake(0, 0, scrennWidth, screenHeight)];
    pickerView.userInteractionEnabled = YES;
    [pickerView canDateAndTime];
    pickerView.butonDelegate = self;
    [self.view addSubview:pickerView];
    pickerView.hidden = YES;
    
    self.sendUrl=[NSString stringWithFormat:addFireControl];

    NSArray *queryDataArr = @[@{@"floorId": @"1",@"floorname":@"火灾报警自检功能",@"rfid":@"<#string#>"},@{@"floorId": @"2",@"floorname":@"消音、复位功能",@"rfid":@"<#string#>"},@{@"floorId": @"3",@"floorname":@"正压风机启动功能",@"rfid":@"<#string#>"},@{@"floorId": @"4",@"floorname":@"排烟风机启动功能",@"rfid":@"<#string#>"},@{@"floorId": @"5",@"floorname":@"正压风阀开启",@"rfid":@"<#string#>"},@{@"floorId": @"6",@"floorname":@"排烟阀控制功能",@"rfid":@"<#string#>"},@{@"floorId": @"7",@"floorname":@"门禁释放功能",@"rfid":@"<#string#>"},@{@"floorId": @"8",@"floorname":@"电梯迫降功能",@"rfid":@"<#string#>"},@{@"floorId": @"9",@"floorname":@"火灾报警装置控制功能",@"rfid":@"<#string#>"},@{@"floorId": @"10",@"floorname":@"接通火灾事故广播功能",@"rfid":@"<#string#>"},@{@"floorId": @"11",@"floorname":@"警铃",@"rfid":@"<#string#>"}];
    self.floorNames = [NSMutableArray array];
    self.floorIds = [NSMutableArray array];
    for (NSDictionary *dic in queryDataArr)
    {
        [self.floorNames addObject:[dic valueForKey:@"floorname"]];
        [self.floorIds addObject:[dic valueForKey:@"floorId"]];
        [self.sendFloorDatas addObject:@""];
        [self.xunJianCellInfos addObject:@{@"hidden":@"1",@"floorId":[dic valueForKey:@"floorId"] }];
    }
    
    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.myTableView selectRowAtIndexPath:index animated:YES scrollPosition:UITableViewScrollPositionMiddle];

    self.allShowDataArr = @[@"检测结果：",@"结论："];
    [self showUIWithArray:self.allShowDataArr andFloorId:@"1"];
    [self.view bringSubviewToFront:self.blackGroundV];
    [self.view bringSubviewToFront:self.blackStopV];
}
- (void)showUIWithArray:(NSArray *)array andFloorId:(NSString *)floorId
{
    [self clearAll];
    self.floorIdQuanju = floorId;
    id receiveFloorObj = [self.sendFloorDatas objectAtIndex:[self.floorIds indexOfObjectss:self.floorIdQuanju]];
    if (![receiveFloorObj isKindOfClass:[NSString class]])
    {
        self.sendCheckDatas = [receiveFloorObj objectForKey:@"checkData"];
    }
    else
    {
        self.sendCheckDatas = [NSMutableArray array];
        [self.sendCheckDatas addObject:@{@"remark": @"",@"content":@""}];
    }
    sendControls = [NSMutableArray array];
    for (int i = 0; i < array.count; i ++ )
    {
        UILabel *checkResult = [UILabel new];
        checkResult.text = [array objectAtIndex:i];
        checkResult.font = [UIFont systemFontOfSize:25];
        CGSize mySz = [checkResult.text sizeWithFont:checkResult.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
        checkResult.frame = CGRectMake(10, [[self.controlWillClears lastObject]frame].origin.y + [[self.controlWillClears lastObject]frame].size.height + interval111, mySz.width, mySz.height);
        [self.backScrollV addSubview:checkResult];
        [self.controlWillClears addObject:checkResult];
        
        UIImageView *tvImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"框"]];
        UITextView *checkTv = [[UITextView alloc]initWithFrame:CGRectMake(60, [[self.controlWillClears lastObject]frame].origin.y + [[self.controlWillClears lastObject]frame].size.height + interval111, 400, 100)];
        tvImageView.userInteractionEnabled = YES;
        if (i == 0)
        {
            checkTv.text = [[[self.sendCheckDatas firstObject]objectForKey:@"remark"]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        }
        else
        {
            checkTv.text = [[[self.sendCheckDatas firstObject]objectForKey:@"content"]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        }
        checkTv.font = [UIFont systemFontOfSize:25];
        checkTv.delegate = self;
        checkTv.backgroundColor = [UIColor clearColor];
        tvImageView.frame = checkTv.frame;
        [self.backScrollV addSubview:tvImageView];
        [self.backScrollV addSubview:checkTv];
        [self.controlWillClears addObject:checkTv];
        [sendControls addObject:checkTv];
    }
    self.commentTv = [UITextView new];
}
- (void)showCalenderSelectView:(id)sender
{
    pickerView.hidden = NO;
    self.navigationController.navigationBar.hidden = YES;
}
- (void)sureOrCancelDelegate:(UIButton *)btn tag:(NSInteger)tag
{
    switch (btn.tag)
    {
        case 0:
        {
            if (![pickerView.timeDateStr isEqualToString: @""])
            {
                timeTextField.text = pickerView.timeDateStr;
            }
        }
            break;
        default:
            break;
    }
    pickerView.hidden = YES;
    self.navigationController.navigationBar.hidden = NO;
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.myTableView) {
        return self.floorNames.count;
    }else{
        for (int j = 0; j<self.tables.count; j++)
        {
            if (tableView == [self.tables objectAtIndex:j])
            {
                return [[self.tableFactors objectAtIndex:j]count];
            }
        }
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [super tableView:tableView cellForRowAtIndexPath:indexPath];
    static NSString *identifier = @"myCell";
    if (tableView == self.myTableView)
    {
        self.mycell.textLabel.text = [self.floorNames objectAtIndex:indexPath.row];
        return self.mycell;
    }
    else
    {
        for (int j = 0; j<self.tables.count; j++)
        {
            if (tableView == [self.tables objectAtIndex:j])
            {
                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
                if (!cell)
                {
                    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
                }
                cell.textLabel.text = [[self.tableValues objectAtIndex:j]objectAtIndex:indexPath.row];
                return cell;
            }
        }
    }
    return nil;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [super tableView:tableView didSelectRowAtIndexPath:indexPath];
    [self hiddenAll];
    if (tableView == self.myTableView)
    {
        [UIView animateWithDuration:.9 animations:^
         {
             self.backScrollV.contentSize = CGSizeMake(0, 0);
         }];
        
        [self showUIWithArray:@[@"检测结果：",@"结论："] andFloorId:[self.floorIds objectAtIndex:indexPath.row]];
        self.floorIdQuanju = [self.floorIds objectAtIndex:indexPath.row];
        
    }
}
#pragma mark - 
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField.tag == 101) {
        self.checkArea = textField.text;
    }
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
    [self.sendCheckDatas replaceObjectAtIndex:0 withObject:@{@"remark": [[[sendControls objectAtIndex:0] text] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],@"content":[[[sendControls objectAtIndex:1] text] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]}];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
